package cn.com.czpb.client.module.news.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.App;
import cn.com.czpb.client.module.news.adapter.ChangeCityAdapter;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.SPUtil;

public class ChangeCityDialog extends Dialog implements View.OnClickListener {

    @BindView(R.id.changename_title)
    TextView changename_title;
    TextView changename_cancel;

    private Activity activity;
    private View view;
    private SPUtil spu;

    private OnChangeCityListener listener;
    public ChangeCityDialog(Context context) {
        super(context);
        activity = (Activity) context;

    }


    public ChangeCityDialog(Context context, int theme) {
        super(context, theme);
        activity = (Activity) context;
        spu = SPUtil.getInstance();

        spu = SPUtil.getInstance();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
    }


    private void init() {
        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        LogUtils.i("width-->" + width);
        view = LayoutInflater.from(activity).inflate(R.layout.dialog_changecity, null);

        setContentView(view);
        changename_title = view.findViewById(R.id.changename_title);
        changename_cancel = view.findViewById(R.id.changename_cancel);

        changename_title.setText("请选择当前城市");
        RecyclerView recy_id = view.findViewById(R.id.recy_id);
        final GridLayoutManager layoutManager = new GridLayoutManager(activity,3);
        recy_id.setLayoutManager(layoutManager);
        ChangeCityAdapter adapter = new ChangeCityAdapter(App.citys);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                String  city = (String) adapter.getItem(position);
                listener.onClick(city);
            }
        });
        recy_id.setAdapter(adapter);

        changename_cancel.setOnClickListener(this);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        lp.width = (int) (0.6*width);
        dialogWindow.setAttributes(lp);



    }

    public void setOnChangeCityListener(OnChangeCityListener listener) {
        this.listener = listener;
    }


    public interface OnChangeCityListener {
        void onClick(String city);
    }
    public void changeName(String str) {

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.changename_cancel:
                dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
//        ChangeNameEvent event = new ChangeNameEvent();
//        event.setNameChange(true);
//        EventBus.getDefault().post(event);
    }

}